/*
   Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
   Original sources are available at www.latestbit.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package org.latestbit.smartobjects.core;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.latestbit.smartobjects.coders.CallInterfaceOperationResponse;
import org.latestbit.smartobjects.core.RemoteSmartObject;
import org.latestbit.smartobjects.exceptions.*;
import org.latestbit.smartobjects.network.ConnectionHandler;
import org.latestbit.smartobjects.network.INetworkCommand;

public class DummyRemoteObject extends RemoteSmartObject {
	private CallInterfaceOperationResponse lastCallResponse = null;
	
	public DummyRemoteObject() throws SmartBusSystemException, InterruptedException {
	}
	
	@Override
	public String getInterfaceId() {
		return DummySyncServerObject.class.getName();
	}

	@Override
	public String[] getSupportedInterfaces() {
		return null;
	}
	
	
	private static final long DEFAULT_AWAIT_TIME = 15;
	private Lock conditionLock = new ReentrantLock();
	private AtomicInteger reponseRcvdCnt = new AtomicInteger(0);
	private AtomicInteger reponseTimeoutCnt = new AtomicInteger(0);
	
	private Condition condition = conditionLock.newCondition();
	
	public void waitCondition() throws InterruptedException, TimeoutException {
		if(!condition.await(DEFAULT_AWAIT_TIME, TimeUnit.SECONDS))
			throw new TimeoutException("Timeout occured for waiting event");
	}
	
	public Lock getConditionLock() {
		return this.conditionLock;
	}
	
	public void signalCondition() {
		conditionLock.lock();
		condition.signal();
		conditionLock.unlock();
	}	

	@Override
	protected void onCallResponseReceived(ConnectionHandler handler,
			INetworkCommand request,
			CallInterfaceOperationResponse callResponse, Object userObject) {

		setLastCallResponse(callResponse);
		signalCondition();
		reponseRcvdCnt.incrementAndGet();
	}

	@Override
	public void onResponseTimeout(INetworkCommand request, Object userObject) {
		setLastCallResponse(null);
		signalCondition();
		reponseTimeoutCnt.incrementAndGet();
	}

	public void setLastCallResponse(CallInterfaceOperationResponse lastCallResponse) {
		this.lastCallResponse = lastCallResponse;
	}

	public CallInterfaceOperationResponse getLastCallResponse() {
		return lastCallResponse;
	}
	
	public int getAsyncCallCountStat() {
		return this.busClient.getCommandProcessor().getAsyncCallsQueueSize();
	}

	public int getResponseReceivedCnt() {
		return this.reponseRcvdCnt.getAndSet(0);
	}
	
	public int getResponseTimeoutCnt() {
		return this.reponseTimeoutCnt.getAndSet(0);
	}		
	
}
